package com.nhh.v.typehandler;

import com.nhh.v.enums.HashRateSource;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedJdbcTypes;
import org.apache.ibatis.type.MappedTypes;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * @author Leonardo Riverh
 * @since 10-17-2017
 */
@MappedJdbcTypes(JdbcType.VARCHAR)
@MappedTypes(HashRateSource.class)
public class HashRateSourceTypeHandler extends BaseTypeHandler<HashRateSource> {

    @Override
    public void setNonNullParameter(PreparedStatement ps, int i, HashRateSource parameter, JdbcType jdbcType) throws SQLException {
        ps.setString(i, parameter.getCode());
    }

    @Override
    public HashRateSource getNullableResult(ResultSet rs, String columnName) throws SQLException {
        return HashRateSource.of(rs.getString(columnName));
    }

    @Override
    public HashRateSource getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
        return HashRateSource.of(rs.getString(columnIndex));
    }

    @Override
    public HashRateSource getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
        return HashRateSource.of(cs.getString(columnIndex));
    }
}
